package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 130: 公安民警危机意念自评量表（Shcrisis），共26题
 */
public class Table130 implements TableCalculate{
    String code = "Shcrisis";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetShcrisisFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return new String[0];
    }

    private static ClsAnswerResult GetShcrisisFactorScore(ClsAnswerRequest answer) {
        // 1绝望因子，2乐观因子，3睡眠因子，4掩饰因子，5自杀意念，6总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 26;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] strClientAnswerA = strAnswer_c[0].toString().split(g_nChar);

            if (strClientAnswerA.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 共2组，第一组1,0，第二组0,1
            String[] strGeneNumA = { "2,3,4,8,11,14,16,17,19,20,23,26", "1,7,10,21,22", "5,12,18,24", "6,9,13,15,25" };

            float[] vFactorA = new float[6]; // 实际为6个因子
            for (i = 0; i < vFactorA.length; i++) {
                vFactorA[i] = 0;
            }
            String[] vDoctorIdeaA = new String[3]; // 实际为3个

            for (i = 0; i < strGeneNumA.length; i++) {
                String[] strSubA = strGeneNumA[i].split(",");
                for (j = 0; j < strSubA.length; j++) {
                    switch (Integer.valueOf(strSubA[j])) {
                        // 选是得1分
                        case 2:
                        case 3:
                        case 4:
                        case 8:
                        case 11:
                        case 12:
                        case 14:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 22:
                        case 23:
                        case 24:
                        case 26:
                            vFactorA[i] += 2 - Integer.valueOf(strClientAnswerA[Integer.valueOf(strSubA[j]) - 1]);
                            break;
                        // '选否得1分
                        case 1:
                        case 5:
                        case 6:
                        case 7:
                        case 9:
                        case 10:
                        case 13:
                        case 15:
                        case 21:
                        case 25:
                            vFactorA[i] += Integer.valueOf(strClientAnswerA[Integer.valueOf(strSubA[j]) - 1]) - 1;
                    }
                }
                vFactorA[5] += vFactorA[i];
            }
            vFactorA[4] = vFactorA[0] + vFactorA[1] + vFactorA[2]; // 自杀意念

            // 总分，自杀意念，掩饰因子
            for (i = 0; i < vDoctorIdeaA.length; i++) {
                vDoctorIdeaA[i] = "Shcrisis." + String.valueOf(i + 1) + ".1";
            }

            ClsScore.Shcrisis shcrisis = new ClsScore.Shcrisis();

            // 评价的顺序是反的，注意
            shcrisis.setDespair_o(vFactorA[0]);
            shcrisis.setOptimism_o(vFactorA[1]);
            shcrisis.setSleep_o(vFactorA[2]);

            shcrisis.setConceal_o(vFactorA[3]);
            shcrisis.setConceal_d(vDoctorIdeaA[2]);

            shcrisis.setSuicide_o(vFactorA[4]);
            shcrisis.setSuicide_d(vDoctorIdeaA[1]);

            shcrisis.setTotal_o(vFactorA[5]);
            shcrisis.setTotal_d(vDoctorIdeaA[0]);

            result.setObject(shcrisis);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }




}
